package com.ruoyi.web.controller.llm;


import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.llm.domain.po.KnowledgeDocInfoPo;
import com.ruoyi.llm.domain.po.KnowledgeInfoPo;
import com.ruoyi.llm.service.KnowledgeDocInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.UUID;

@Api(tags="知识库文档接口")
@RestController
@RequestMapping("/llm/knowledgeDoc")
public class KnowledgeDocController extends BaseController {


    public static final Logger log = LoggerFactory.getLogger(KnowledgeDocController.class);


    @Autowired
    private KnowledgeDocInfoService knowledgeDocInfoService;

    @PostConstruct
    public void init(){
//        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~   KnowledgeInfoController   ~~~");
    }


    @GetMapping("/list")
    @ApiOperation(value = "获取文档列表",response= KnowledgeDocInfoPo.class)
    public TableDataInfo knowledgeList(@RequestParam("knowId") String knowId, Integer pageNum, Integer pageSize)
    {
        startPage();
        if(pageSize == null){
            pageSize = 10;
        }
        if(pageNum == null){
            pageNum = 1;
        }
        PageHelper.startPage(pageNum,pageSize);
        List<KnowledgeDocInfoPo> ls=knowledgeDocInfoService.knowledgeDocList(knowId);
        return getDataTable(ls);
    }

    @GetMapping("/getDocById")
    @ApiOperation(value = "获取知识库文档",response= KnowledgeDocInfoPo.class)
    public AjaxResult getDocById(@RequestParam("docId") String docId)
    {
        KnowledgeDocInfoPo doc=knowledgeDocInfoService.getknowledgeDoc(docId);
        return this.success(doc);
    }


    /**
     * 文档上传
     */
    @ApiOperation(value = "文档上传")
    @PostMapping("/docUpload")
    public AjaxResult avatar(@RequestParam("doc") MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            String doc_path = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.DOC_EXTENSION);

            doc_path=RuoYiConfig.getProfile()+doc_path;

            AjaxResult ajax = AjaxResult.success();
            ajax.put("doc_path", doc_path);
            return ajax;

        }else{
            return error("文档不能为空！");
        }

    }


    @ApiOperation(value = "开始导入")
    @PostMapping("/startImport")
    public AjaxResult startImport(@RequestBody JSONArray json) throws Exception
    {
        String taskId = UUID.randomUUID()+"";
        JSONObject sendJson = new JSONObject();
        sendJson.put("taskData",json);
        sendJson.put("taskId",taskId);

        return this.success(knowledgeDocInfoService.sentMQ(taskId,sendJson));

    }

}
